﻿using FasterWPF;
using FasterWPFDemo.UserControls;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Shapes;
using System.Reactive;
using System.Reactive.Linq;

namespace FasterWPFDemo
{
    //Plain DataGrid
    public partial class TabItem08 : UserControl, IUserControlGuid
    {
        private string _userCtlGuid = null;

        public string UserControlGuid
        {
            get
            {
                return _userCtlGuid;
            }

            set
            {
                _userCtlGuid = value;
            }
        }

        DataGrid dg = null;
        string rowGuid7 = null;

        public TabItem08()
        {
            //InitializeComponent(); XAML removed. Initialize via code-behind.

            this.Initialize(ContainerType.VerticalPanel);

            this.Width = 550D;
            this.Height = 700D;

            this.BeginSettings<UserControl>().SetItemBorderColorAndThickness<UserControl>(Brushes.Gray, new Thickness(3))
                .SetSelectorClass<UserControl>(0, 0, "tabLabel")
                .SetFontOnLabel<UserControl>(0, 0, "Segoe UI", 12D, FontWeights.Normal, FontStyles.Normal)
                .SetTextColorForLabel<UserControl>(0, 0, Brushes.White)
                .EndSettings();
        }

        public void LoadUserControl()
        { 
            dg = new DataGrid();
            dg.RowHeaderWidth = 0;
            dg.SetRegularRowColor(Brushes.SeaGreen);
            dg.SetSelectedContentControlColor(Brushes.LightCoral);
            dg.SetSelectedContentControlForeground(Brushes.Purple);
            dg.SetSelectedFocusBorder(Brushes.Purple);

            //Since a row in a datagrid typically corresponds to only a single guid, let's contrive keys for each column
            //based on a single common guid. Following this pattern is advised . . .
            string rowGuid0 = Guid.NewGuid().ToString();
            string cellGuid1 = rowGuid0 + "1";
            string cellGuid2 = rowGuid0 + "2";
            string rowGuid1 = Guid.NewGuid().ToString();
            string cellGuid3 = rowGuid1 + "1";
            string cellGuid4 = rowGuid1 + "2";
            string rowGuid2 = Guid.NewGuid().ToString();
            string cellGuid5 = rowGuid2 + "1";
            string cellGuid6 = rowGuid2 + "2";

            dg.Initialize(428D, 375.0D, ContainerType.Grid, Brushes.LightSteelBlue, Brushes.Black, Brushes.Black, new Thickness(0), 5, 3);
            dg.SetCompositeGridDimensions<DataGrid>(3, 1, 155D, GridUnitType.Pixel, 23D, GridUnitType.Pixel);
            dg.BeginSettings<DataGrid>().SetBackgroundColorOnItemsControl(Brushes.LightSlateGray)
                .SetItemBorderSettings(double.NaN, double.NaN, Brushes.Silver, new Thickness(2)).Set<Rectangle, DataGrid>(0, 0, "Height", 25D)
                .SetTextPadding(0, 0, new Thickness(3))
                .SetTextPadding(1, 0, new Thickness(3))
                .SetTextPadding(2, 0, new Thickness(3))
                .Set<Rectangle, DataGrid>(1, 0, "Width", 25D)
                .Set<Rectangle, DataGrid>(1, 0, "Fill", Brushes.Green)
                .EndSettings<DataGrid>();

            //myGrid.BeginComposite<Grid>(gridguid6)
            //    .AddExisting<DataGrid, Grid>(0, 0, dg)
            //    .EndComposite<Grid, GridArgs>(new GridArgs(1, 1));

            List<string> columnHeaderList = new List<String>();
            columnHeaderList.Add(" IL Attractions");
            columnHeaderList.Add(" Directions");

            //only need to pass in column header names (columnHeaderList) once
            DataSet ds = new DataSet("testDs");
            DataTable dsTestTable = new DataTable("Test");

            #region raw example using a dataset outside of WPF Composites - not recommended unless using XceedGridExt?
            //ds.Tables.Add(dsTestTable);
            //dsTestTable.Columns.Add(new DataColumn("Column 1", typeof(string)));
            //dsTestTable.Columns.Add(new DataColumn("Column 2", typeof(string)));
            //DataRow row1 = ds.Tables[0].NewRow();
            //DataRow row2 = ds.Tables[0].NewRow();
            ////row2.ItemArray = new string[] { "row 2 - string in col 1", "string in col 2" };
            //row1["Column 1"] = "row 1 -string in col 1";
            //row1["Column 2"] = "string in col 2";
            //row2["Column 1"] = "row 2 -string in col 1";
            //row2["Column 2"] = "string in col 2";
            //dsTestTable.Rows.Add( row1 );
            //dsTestTable.Rows.Add( row2 );
            //dg.AutoGenerateColumns = true;
            ////dg.DataContext = ds.Tables[0].DefaultView;
            //dg.ItemsSource = ds.Tables[0].DefaultView;
            #endregion

            Binding b = new Binding()
            {
                Source = this
            };
            dg.SetBinding(DataGrid.ItemsSourceProperty, new Binding());

            //Add composite to row 0 col 0
            dg.BeginComposite<DataGrid>(cellGuid1)
                .AddText(0, 0, "Lake Michigan")
                .AddText(1, 0, "Chicago")
                .AddText(2, 0, "IL")
                .EndComposite(new DataGridArgs(rowGuid0, 0, 0, columnHeaderList));

            //Set this before applying column styles!!!
            ScrollViewerStyle.ApplyToScrollViewer<DataGrid>(dg, "PowderBlue", "Blue", "Blue");

            //Since columns have been added, set their minimum widths and corner color. Must be done after column headers added.
            dg.Columns[0].MinWidth = 200D;
            dg.Columns[1].MinWidth = 200D;
            dg.SetBackgroundColor(Brushes.Purple);
            dg.SetColumnHeaderForeground(Brushes.White);
            dg.SetColumnHeaderColor(Brushes.RoyalBlue);

            //Add composite to row 0 col 1
            dg.BeginComposite<DataGrid>(cellGuid2)
                .AddText(0, 0, "999")
                .AddText(1, 0, "This way 1")
                .AddText(2, 0, "")
                .EndComposite(new DataGridArgs(rowGuid0, 0, 1));

            //ROW 1
            //Add composite to row 1 col 0
            dg.BeginComposite<DataGrid>(cellGuid3)
                .AddText<DataGrid>(0, 0, "Lake Michigan")
                .AddAnything<Rectangle, DataGrid>(1, 0)
                .AddText(2, 0, "Lisle, IL")
                .EndComposite(new DataGridArgs(rowGuid1, 1, 0));
            //Add composite to row 1 col 1
            dg.BeginComposite<DataGrid>(cellGuid4)
                .AddText(0, 0, "Take I-94")
                .AddText(1, 0, "North 2")
                .AddText(2, 0, "")
                .EndComposite(new DataGridArgs(rowGuid1, 1, 1));

            //ROW 2
            //Add composite to row 2 col 0
            dg.BeginComposite<DataGrid>(cellGuid5)
                .AddText(0, 0, "Lincoln Museum")
                .AddText(1, 0, "Springfield")
                .AddText(2, 0, "IL")
                .EndComposite(new DataGridArgs(rowGuid2, 2, 0));
            //Add composite to row 2 col 1
            dg.BeginComposite<DataGrid>(cellGuid6)
                .AddText(0, 0, "Hop on the")
                .AddText(1, 0, "Edens Expressway 3")
                .AddText(2, 0, "heading South")
                .EndComposite(new DataGridArgs(rowGuid2, 2, 1));

            //ROW 3
            //Add composite to row 3 col 0
            string rowGuid3 = Guid.NewGuid().ToString();
            dg.BeginComposite<DataGrid>(rowGuid3 + "1")
                .AddText(0, 0, "Lincoln Museum 3")
                .AddText(1, 0, "Springfield")
                .AddText(2, 0, "IL")
                .EndComposite(new DataGridArgs(rowGuid3, 3, 0));
            //Add composite to row 3 col 1
            dg.BeginComposite<DataGrid>(rowGuid3 + "2")
                .AddText(0, 0, "999")
                .AddText(1, 0, "another way 4")
                .AddText(2, 0, "")
                .EndComposite(new DataGridArgs(rowGuid3, 3, 1));

            //ROW 4
            //Add composite to row 4 col 0
            string rowGuid4 = Guid.NewGuid().ToString();
            Border b4_0 = dg.BeginComposite<DataGrid>(rowGuid4 + "1")
                .AddText(0, 0, "Lincoln Museum 4")
                .AddText(1, 0, "Springfield")
                .AddText(2, 0, "IL")
                .EndComposite(new DataGridArgs(rowGuid4, 4, 0));
            //Add composite to row 4 col 1
            dg.BeginComposite<DataGrid>(rowGuid4 + "2")
                .AddText(0, 0, "999")
                .AddText(1, 0, "Compose Observable")
                .AddText(2, 0, "1")
                .SubscribeEventOnChild<TextBlock, DataGrid, MouseButtonEventArgs>(2, 0, "PreviewMouseLeftButtonDown", 
                ((o, e) => { FasterWPF.TextBlockExt.SetText((o as TextBlock), ((Int32.Parse((o as TextBlock).GetText())) + 1).ToString()); }),
                (connectableObservable, actionDelegate) => { connectableObservable.Skip(2).Subscribe(actionDelegate); })                  
                .EndComposite(new DataGridArgs(rowGuid4, 4, 1));

            //ROW 5
            //Add composite to row 5 col 0
            string rowGuid5 = Guid.NewGuid().ToString();
            Border b5_0 = dg.BeginComposite<DataGrid>(rowGuid5 + "1")
                .AddText(0, 0, "Lincoln Museum 5")
                .AddText(1, 0, "Springfield1")
                .AddText(2, 0, "IL")
                .EndComposite(new DataGridArgs(rowGuid5, 5, 0));
            //Add composite to row 5 col 1
            dg.BeginComposite<DataGrid>(rowGuid5 + "2")
                .AddText(0, 0, "999")
                .AddText(1, 0, "another way 5")
                .AddText(2, 0, "")
                .EndComposite(new DataGridArgs(rowGuid5, 5, 1));

            //ROW 6
            //Add composite to row 6 col 0
            string rowGuid6 = Guid.NewGuid().ToString();
            Border b6_0 = dg.BeginComposite<DataGrid>(rowGuid6 + "1")
                .AddText(0, 0, "Lincoln Museum 5")
                .AddText(1, 0, "Springfield3")
                .AddText(2, 0, "IL")
                .EndComposite(new DataGridArgs(rowGuid6, 6, 0));
            //Add composite to row 6 col 1
            dg.BeginComposite<DataGrid>(rowGuid6 + "2")
                .AddText(0, 0, "999")
                .AddText(1, 0, "another way 6")
                .AddText(2, 0, "")
                .EndComposite(new DataGridArgs(rowGuid6, 6, 1));

            //ROW 7
            //Add composite to row 7 col 0
            rowGuid7 = Guid.NewGuid().ToString();
            Border b7_0 = dg.BeginComposite<DataGrid>(rowGuid7 + "1")
                .AddText(0, 0, "Lincoln Museum 5")
                .AddText(1, 0, "Springfield2")
                .SubscribeMouseSingleClickOnChild<TextBlock, DataGrid>(1, 0, (o3, e) => { MessageBox.Show("yo!"); })
                .AddText(2, 0, "IL")
                .EndComposite(new DataGridArgs(rowGuid7, 7, 0));
            //Add composite to row 7 col 1
            dg.BeginComposite<DataGrid>(rowGuid7 + "2")
                .AddText(0, 0, "999")
                .AddText(1, 0, "another way 7")
                .AddText(2, 0, "Magic Inc.")
                .SetSelectorClassAtXY<TextBlock, DataGrid>(2, 0, FasterWPF.SettingsManager.SelectorPosition.One, "magic")
                //.ModifyAtXY<TextBlock, DataGrid>(2, 0, txtBlck=>{txtBlck.s })
                .EndComposite(new DataGridArgs(rowGuid7, 7, 1));
            dg.SetSelectorClass("MagicParent");

            dg.Refresh();
            Button b3 = new Button();
            b3.Content = "Sort DataGrid by 0,0 DESC and 1,0 ASC in Column IL Attractions";
            b3.Click += a_Click;
            b3.MaxHeight = 50;

            this.BeginComposite<UserControl>(_userCtlGuid)
                .AddLabel<UserControl>(0, 0, "Tab Item 8 -  Plain WPF DataGrid Example", Brushes.Navy)
                .AddLabel<UserControl>(0, 1, "", Brushes.AliceBlue)
                .AddExisting<Button, UserControl>(0, 2, b3)
                .AddLabel<UserControl>(0, 3, "", Brushes.AliceBlue)
                .AddExisting<DataGrid, UserControl>(0, 4, dg)
                .EndComposite<UserControl, UserControlArgs>(null);

            //Update anything by guid row col or by just row col . . .
            dg.Update<TextBlock, DataGrid>(2, 0, txb1 =>
            {
                txb1.Background = Brushes.AliceBlue;
            });

            //Rectangle was directly set to Green earlier so an UpdateViaStyle won't change it unless you clear the value first
            dg.UpdateByKey<Rectangle, DataGrid>(cellGuid3, 1, 0, txb1 =>
            {
                txb1.ClearValue(Rectangle.FillProperty);
            });

            //Since we cleared the value we can now set it via a style. Of course, we also could have set it directly again.
            //This is just to illustrate order of precedence and one approach to working with it
            dg.UpdateViaStyleByKey<Rectangle, DataGrid>(cellGuid3, 1, 0, typeof(Rectangle), Rectangle.FillProperty, BrushExt.CreateGradientBrush("Blue", 0, "AliceBlue", .7, 0, 0, 0, 1));
            
        }
    }
}
